package org.springblade.webservice;


import org.apache.cxf.endpoint.Client;
import org.apache.cxf.jaxws.endpoint.dynamic.JaxWsDynamicClientFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springblade.core.tool.jackson.JsonUtil;
import org.springblade.mach.entity.ProjectMana;
import org.springblade.mach.service.IProjectManaService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;

import javax.xml.namespace.QName;
import java.util.List;

/**
 * @author LY
 * @version 1.0
 * @date 2021/4/23 14:12
 */

@Configuration
@EnableScheduling
public class WebServiceClient {
	@Autowired
	private IProjectManaService projectManaService;

	private Logger logger = LoggerFactory.getLogger(WebServiceClient.class);
	private Client client = null;

	@Scheduled(cron = "0 0 0/1 * * ?")
	public void getCustomerList() throws Exception {
		final long start = System.currentTimeMillis();
		JaxWsDynamicClientFactory dcf = JaxWsDynamicClientFactory.newInstance();
		if (client == null) {
			client = dcf.createClient("http://192.168.1.243:8280/machflow/webservice/API?wsdl");
		}
		QName operationName = new QName("http://cxf.mach.modules.jeesite.thinkgem.com/", "technicalProject");
		Object[] result = client.invoke(operationName);
		String jsonList = (String) result[0];
		List<ServerEntity> serverEntityList = JsonUtil.parseArray(jsonList, ServerEntity.class);
		serverEntityList.forEach(serverEntity -> {
			ProjectMana projectMana = new ProjectMana();
			projectMana.setCustomerName(serverEntity.getCustomerName());
			projectMana.setProjectName(serverEntity.getEqName());
			projectMana.setWorkOrderNo(serverEntity.getCode());
			boolean save = projectManaService.save(projectMana);
			if (save) {
				QName update = new QName("http://cxf.mach.modules.jeesite.thinkgem.com/", "updateTechnical");
				try {
					client.invoke(update, serverEntity.getId());
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
		final long end = System.currentTimeMillis();
		if (serverEntityList.size() > 0) {
			logger.info("获取经营计划管理系统项目信息方法执行成功,获取数据{}条,耗时:{}", serverEntityList.size(), end - start);
		}
	}
}

